<template>
  <div class="mod-log">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" id="header">
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()" type="primary" size="mini">查询</el-button>
      </el-form-item>
      <el-form-item class="outExcel">
        <el-button @click="excelto()" type="primary" size="mini">导出Excel</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      :default-sort="{prop: 'date', order: 'descending'}"
      v-loading="dataListLoading"
      :header-cell-style="headFirst"
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column prop="operationDate" label="操作时间" sortable width="180"></el-table-column>
      <el-table-column prop="operationType" label="操作功能" width="180"></el-table-column>
      <el-table-column prop="operationContent" label="操作详情"></el-table-column>
      <el-table-column prop="pkLogId" label="操作人员"></el-table-column>
      <el-table-column prop="ip" label="操作主机"></el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      filename: '日志列表',
      pageIndex: 1,
      tableData: [],
      pageSize: 10,
      pageNum: 1,
      totalPage: 0,
      dataListLoading: false,
      searchInfo: {},
      startTime: '',
      endTime: '',
      searchInfo: {},
      userGuid: '',
      recordCount: 0,
      currentPage: 1,
      exportpam: {},
      logexportlist: [],
      datetime: [],
      time: [],
      timeDefaultShow: []
    }
  },
  created() {
    this.userGuid = sessionStorage.getItem('administrator')

    // 初始化时间
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  mounted() {},
  methods: {
    //获取当前
    getmoontime() {},
    excelto() {
      if (this.tableData.length == 0) {
        this.$message({
          message: '查询数据后导出',
          type: 'warning'
        })
        return false
      }
      this.outexecel()
    },
    outexecel() {
      this.$http({
        url: this.$http.adornUrl('/manager/logger/info-page'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageSize: 9999,
            searchInfo: this.exportpam
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          let jsonArr = data.messageContent.data
          this.logexportlist = jsonArr
          setTimeout(() => {
            this.export2Excel()
          }, 100)
        }
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    export2Excel() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const multiHeader = []
        const multiHeader2 = []
        const merges = []
        const { export_json_to_excel } = require('../../excel/Export2Excel')
        const tHeader = [
          '操作时间',
          '操作功能',
          '操作详情',
          '操作人员',
          '操作主机'
        ]
        const filterVal = [
          'operationDate',
          'operationType',
          'operationContent',
          'pkLogId',
          'ip'
        ]
        const list = this.logexportlist
        console.log(list)
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: this.filename + nowtime
        })
      })
    },
    getDataList() {
      if (this.datetime == '') {
        this.$message({
          message: '请选择时间',
          type: 'warning'
        })
        return
      }
      this.searchInfo = {}
      this.searchInfo.startTime = this.datetime[0] + ' ' + this.time[0]
      this.searchInfo.endTime = this.datetime[1] + ' ' + this.time[1]
      if (this.userGuid != '') {
        this.searchInfo.userGuid = this.userGuid
      }
      this.exportpam = this.searchInfo
      this.pageNum = 1
      this.init()
    },
    init() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/manager/logger/info-page'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: 0
        })
      }).then(({ data }) => {
        console.log(data)
        this.dataListLoading = false
        if (data.messageCode === 200) {
          this.tableData = data.messageContent.data
          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
          return
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    formatter(row, column) {
      return row.address
    },
    //分类页
    handleSizeChange(val) {
      this.pageSize = val
      this.init()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>
.mod-log {
  padding: 10px;
  .el-form--inline .el-form-item{
    margin-bottom: 5px;
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>